package com.water.tool;

/**
 * 自定义标签
 */
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.intranet.sys.domain.Menu;
import com.intranet.sys.service.MenuService;

@Controller
public class WaterTag extends TagSupport {

	@Autowired
	private MenuService menuService;
	
	private String parameter;

	public String getParameter() {
		return parameter;
	}

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	@Override
	public int doStartTag() throws JspException {
		try {
			HttpServletRequest request = (HttpServletRequest) pageContext
					.getRequest();
	
			JspWriter out = pageContext.getOut();
			
			if (parameter.compareToIgnoreCase("li") == 0) {
				List<Menu> menus =  (List<Menu>) request.getSession().getAttribute("leftMenus");
				StringBuffer sb = new StringBuffer();
				for (Menu pm : menus) {
					Set<Menu> cmenus = pm.getMenus();
					sb.append("<li class=''><a href='javascript:initContent(\""+pm.getUrl()+"&menuPath="+pm.getAccesspath()+"\")'> <i class='icon-home'></i><span class='title'>"+pm.getName()+"</span>");
					if (cmenus.size() > 0) {
						sb.append("<span class='arrow'></span>");
					}
					sb.append("</a>");
					if (cmenus.size() > 0) {
						sb.append("<ul class='sub-menu'>");
						for (Menu cm : cmenus) {
							sb
									.append("<li><a href='javascript:initContent(\""+cm.getUrl()+"&menuPath="+cm.getAccesspath()+"\")'>"
											+ cm.getName() + "</a></li>");
						}
						sb.append("</ul>");
					}
					sb.append("</li>");
				}
				out.print(sb.toString());
			} else {
				out.print(new Date());
			}
		} catch (IOException e) {
			throw new JspTagException(e.getMessage());
		}
		return SKIP_BODY;
	}

}
